<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    width="500px"
    @callBack="onCallBack"
    @close="onClose"
  >
    <el-form
      ref="ruleForm"
      :model="form"
      :rules="rules"
      label-width="120px"
      label-position="left"
    >
      <el-form-item label="收件人" prop="receiver">
        <el-input
          v-model="form.receiver"
          clearable
          placeholder="请输入收件人名称"
        />
      </el-form-item>

      <el-form-item label="所属公司" prop="company">
        <el-input
          v-model="form.company"
          clearable
          placeholder="请输入所属公司"
        />
      </el-form-item>

       <el-form-item label="地址" prop="address">
        <el-input v-model="form.address" clearable placeholder="请输入收件地址" />
      </el-form-item>

      <el-form-item label="邮编" prop="post">
        <el-input v-model="form.post" clearable placeholder="请输入收件邮编" />
      </el-form-item>

      <el-form-item label="电话" prop="phone">
        <el-input v-model="form.phone" clearable placeholder="请输入收件电话" />
      </el-form-item>

      <el-form-item label="支付方式" prop="payMethod">
        <el-radio-group
          v-model="form.payMethod"
          clearable
          placeholder="请选择支付方式"
        >
          <el-radio
            v-for="item in options"
            :key="item.value"
            :label="item.value"
            >{{ item.label }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOne, save } from "@/api/sysExpress";

export default {
  data() {
    return {
      dialogVisible: false,
      title: "添加",
      loading: false,
      options: [
        {
          value: "1",
          label: "月结",
        },
        {
          value: "2",
          label: "到付",
        },
      ],
      form: {
        id: "",
        receiver: "",
        company: "",
        address: "",
        post: "",
        phone: "",
        payMethod: "1",
      },
      rules: {
        receiver: [
          {
            required: true,
            message: "请填写收件人",
            trigger: "blur",
          },
        ],
        company: [
          {
            required: true,
            message: "请填写收件人公司",
            trigger: "blur",
          },
        ],
        address: [
          {
            required: true,
            message: "请输入收件人地址",
            trigger: "blur",
          },
        ],
        post: [
          {
            required: true,
            message: "请输入收件人邮编",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请填写收件人联系方式",
            trigger: "blur",
          },
        ],
        payMethod: [
          {
            required: true,
            message: "请选择支付方式",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init() {
      if (this.form.id) {
        getOne(this.form.id).then((res) => {
          if (res && res.code === 20000) {
            const rtn = res.data;
            this.form.id = rtn.id;
            this.form.receiver = rtn.receiver;
            this.form.company = rtn.company;
            this.form.address = rtn.address;
            this.form.post = rtn.post;
            this.form.phone = rtn.phone;
          }
        });
      }
    },
    onShow(id) {
      if (id) {
        this.form.id = id;
        this.title = "编辑";
        this.init();
      } else {
        this.title = "添加";
      }
      this.dialogVisible = true;
    },
    onClose() {
      this.$refs["ruleForm"].resetFields();
      this.form = this.$options.data().form;
      this.dialogVisible = false;
    },
    onSubmit() {
      this.$refs["ruleForm"].validate((valid) => {
        console.log(this.form);
        if (valid) {
          save(this.form)
            .then((res) => {
              console.log(res);
              if (res && res.code === 20000) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                this.dialogVisible = false;
                this.onCallBack();
              }
            })
            .catch((e) => {});
        } else {
          return false;
        }
      });
    },
    onCallBack() {
      this.$emit("onCallBack");
    },
  },
};
</script>

<style scoped>
.el-form {
  padding-left: 30px;
  padding-right: 30px;
}
</style>
